using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Plugin;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Navigation;
namespace MicroMsg
{
	public class App : Application
	{
		public static bool sBAppIsInForground = false;
		public static bool AppGoingToFirstPage = true;
		public static long unhandledExceptionTimes = 0L;
		public static ApplicationUnhandledExceptionEventArgs lastException;
		private bool phoneApplicationInitialized;
		private bool _contentLoaded;
		public PhoneApplicationFrame RootFrame
		{
			get;
			private set;
		}
		public App()
		{
			Profiler.setPoint("AppStart");
			base.add_Startup(new StartupEventHandler(this.App_Startup));
			base.add_UnhandledException(new EventHandler<ApplicationUnhandledExceptionEventArgs>(this.Application_UnhandledException));
			Log.OutputToFile = true;
			Log.Level = 1;
			BackgroundService.StopPeriodicAgent();
			this.InitializeComponent();
			Profiler.setPoint("initComponent");
			this.InitializePhoneApplication();
			Profiler.setPoint("initPhone");
			if (Debugger.get_IsAttached())
			{
				PhoneApplicationService.get_Current().set_UserIdleDetectionMode(1);
			}
			ServiceCenter.init();
			Profiler.setPoint("ServiceCenter");
			ExtentCenter.initialize();
			Profiler.setPoint("ExtentCenter");
			TileManager.CreateApplicationTile();
			Profiler.setPoint("CreateTile");
			if (GConfigMgr.settings.lastMainVersion != ConstantsProtocol.CLIENT_VERSION)
			{
				GConfigMgr.settings.lastMainVersion = ConstantsProtocol.CLIENT_VERSION;
				GConfigMgr.saveSetting();
				SessionPackMgr.setSessionKey(null);
			}
			string text = CultureInfo.get_CurrentUICulture().ToString();
			string text2 = text;
			bool flag = false;
			if (text != GConfigMgr.settings.strLastSystemLanguage)
			{
				GConfigMgr.settings.strUserSetLanguage = text;
				GConfigMgr.settings.strLastSystemLanguage = text;
				GConfigMgr.saveSetting();
				flag = true;
			}
			else
			{
				if (text != GConfigMgr.settings.strUserSetLanguage)
				{
					text2 = GConfigMgr.settings.strUserSetLanguage;
				}
			}
			if (flag)
			{
				SessionPackMgr.setSessionKey(null);
			}
			Profiler.setPoint("setLanguage");
			CultureInfo culture = new CultureInfo(text2);
			Profiler.setPoint("CultureInfo");
			strings.Culture = culture;
		}
		private void gotoFirstPage()
		{
			if (AccountMgr.hasValidAccount())
			{
				MainPage.GotoThisPage(new object[]
				{
					true
				}, false);
				return;
			}
			ConfigMgr.setBool("HasRequestSnsGroupKey", false);
			MMTheme.GetInstance().DelayInitThemes();
			string text = "/Source/UI/LoginViews/LoginMainView.xaml";
			((App)Application.get_Current()).RootFrame.Navigate(new Uri(text, 2));
		}
		private void gotoFirstPageFromContentShare(string string_0)
		{
			MMTheme.GetInstance().DelayInitThemes();
			if (AccountMgr.hasValidAccount())
			{
				string text = "/Source/UI/ChoosePeopleToShareContent.xaml?" + string_0;
				((App)Application.get_Current()).RootFrame.Navigate(new Uri(text, 2));
				return;
			}
			string text2 = "/Source/UI/LoginViews/LoginMainView.xaml?" + string_0;
			((App)Application.get_Current()).RootFrame.Navigate(new Uri(text2, 2));
		}
		private void App_Startup(object sender, StartupEventArgs e)
		{
			base.get_Resources().Add("R", new R());
			base.get_Resources().Add("strings", new strings());
		}
		private void Application_Launching(object sender, LaunchingEventArgs e)
		{
			Log.smethod_5("APP", "Application_Launching...  ");
			App.sBAppIsInForground = true;
			BasePage.EnableBackSysTask();
		}
		private void Application_Activated(object sender, ActivatedEventArgs e)
		{
			App.sBAppIsInForground = true;
			BasePage.EnableBackSysTask();
			NetHandler.onApplicationActivated();
			R.AttachSysTheme();
			ExtentCenter.onAppActivated();
			if (AccountMgr.isLogin)
			{
				AccountMgr.checkSessionKeyTimeout();
				EventCenter.postEvent(EventConst.ON_APP_ACTIVE, null, null);
			}
			BackgroundService.StopPeriodicAgent();
		}
		private void Application_Deactivated(object sender, DeactivatedEventArgs e)
		{
			App.sBAppIsInForground = false;
			Log.smethod_5(base.GetType().ToString(), "Application_Deactivated");
			ExtentCenter.onAppDeactivated();
			MainPage.UnInitProgram();
			NetHandler.onApplicationDeactivated();
			CrashLogMgr.unSetCrash();
			if (AccountMgr.isLogin)
			{
				BackgroundService.StartPeriodicAgent();
			}
		}
		private void Application_Closing(object sender, ClosingEventArgs e)
		{
			Log.smethod_5(base.GetType().ToString(), "Application_Closing");
			MainPage.UnInitProgram();
			ExtentCenter.uninitialize();
			ServiceCenter.uninit();
			CrashLogMgr.unSetCrash();
			Log.smethod_7();
		}
		public void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
		{
			Log.smethod_5("APP", "RootFrame_Navigating  in...");
			Log.smethod_5("APP", string.Concat(new object[]
			{
				"AppGoingToFirstPage == ",
				App.AppGoingToFirstPage,
				" && ",
				e.get_Uri().ToString()
			}));
			if (App.AppGoingToFirstPage && e.get_Uri().ToString().Contains("//Source/UI/MainPage.xaml"))
			{
				App.AppGoingToFirstPage = false;
				int num = e.get_Uri().get_OriginalString().IndexOf('?');
				string string_0 = "";
				if (num >= 0)
				{
					string_0 = e.get_Uri().get_OriginalString().Substring(num + 1);
				}
				Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
				if (string_0.get_Length() != 0)
				{
					string[] array = string_0.Split(new char[]
					{
						'&'
					});
					for (int i = 0; i < array.Length; i++)
					{
						string text = array[i];
						dictionary.set_Item(text, true);
					}
				}
				if (dictionary.ContainsKey("Action=ShareContent"))
				{
					Log.smethod_5("APP", "RootFrame_Navigating...  From ShareContent");
					e.set_Cancel(true);
					this.RootFrame.get_Dispatcher().BeginInvoke(delegate
					{
						this.gotoFirstPageFromContentShare(string_0);
					});
					return;
				}
				Log.smethod_5("APP", "RootFrame_Navigating...  From APP List");
				e.set_Cancel(true);
				this.RootFrame.get_Dispatcher().BeginInvoke(delegate
				{
					this.gotoFirstPage();
				});
			}
		}
		private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
		{
			Log.smethod_1("APP", "msg:" + e.get_Exception().get_StackTrace());
			if (Debugger.get_IsAttached())
			{
				Debugger.Break();
			}
		}
		private void testException()
		{
			base.add_UnhandledException(new EventHandler<ApplicationUnhandledExceptionEventArgs>(this.Test_UnhandledException));
			Thread thread = new Thread(delegate
			{
				throw new TestException();
			});
			thread.Start();
			Log.smethod_5("APP", "main threadid=" + Thread.get_CurrentThread().get_ManagedThreadId());
		}
		private void Test_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			TestException arg_0B_0 = e.get_ExceptionObject() as TestException;
		}
		private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			if (e.get_ExceptionObject() is QuitException)
			{
				return;
			}
			if (e.get_ExceptionObject().get_Message().Contains("Camera initialization failed"))
			{
				e.set_Handled(true);
				return;
			}
			HostService.printInfo();
			Connector.printInfo();
			AppInfoHelper.printfInfo();
			App.unhandledExceptionTimes += 1L;
			App.lastException = e;
			Log.smethod_5("APP", string.Concat(new object[]
			{
				"unhandledExceptionTimes = ",
				App.unhandledExceptionTimes,
				",threadid=",
				Thread.get_CurrentThread().get_ManagedThreadId()
			}));
			Log.smethod_2("APP", "Stack: " + e.get_ExceptionObject().get_StackTrace(), e.get_ExceptionObject());
			if (Debugger.get_IsAttached())
			{
				Debugger.Break();
			}
			CrashLogMgr.setCrash(e.get_ExceptionObject().get_StackTrace() + e.get_ExceptionObject());
			Log.smethod_7();
		}
		private void InitializePhoneApplication()
		{
			if (this.phoneApplicationInitialized)
			{
				return;
			}
			this.RootFrame = new TransitionFrame();
			this.RootFrame.add_Navigated(new NavigatedEventHandler(this.CompleteInitializePhoneApplication));
			this.RootFrame.add_NavigationFailed(new NavigationFailedEventHandler(this.RootFrame_NavigationFailed));
			this.RootFrame.add_Navigating(new NavigatingCancelEventHandler(this.RootFrame_Navigating));
			this.phoneApplicationInitialized = true;
		}
		private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
		{
			if (base.get_RootVisual() != this.RootFrame)
			{
				base.set_RootVisual(this.RootFrame);
			}
			this.RootFrame.remove_Navigated(new NavigatedEventHandler(this.CompleteInitializePhoneApplication));
		}
		public static void ExitProgram()
		{
			MainPage.UnInitProgram();
			CrashLogMgr.unSetCrash();
			Log.smethod_7();
			if (AccountMgr.isLogin)
			{
				BackgroundService.StartPeriodicAgent();
			}
			throw new QuitException();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/App.xaml", 2));
		}
	}
}
